
/// SKELMESHANIM source code
/// by Alvaro Cuno, LCG-COPPE-UFRJ
/// bugs? email-me: alvaroecp@gmail.com
/// November, 2007

#include "motion.h"

Motion::Motion(const char* filename) {
   
   // Load the poses 
   ifstream posesfile(filename);
   if (not posesfile.is_open()) {
      std::cout<<"I/O error: grrr the file doesn't exist "<<std::endl;
      return;
   }
   
   string posesformat;
   int nposes, nconstraints;
   // Load header
   posesfile>>posesformat;
   assert(posesformat=="POSES_SKEL" or posesformat=="POSES_MESH");
   posesfile>>nposes>>nconstraints;
   
   cout<<posesformat<<" "<<nposes<<" "<<nconstraints<<endl;
   
   // Load the constraint identifiers
   vector<int> cnstr(nconstraints); // constra
   for (int i=0; i<nconstraints; ++i) {
      posesfile>>cnstr[i];
   }
   
   // Reserve memory
   displacements.reserve(nposes);
   // Load the data
   Number x, y, z;
   for (unsigned int i=0; i<nposes; ++i) {
      vector<Vector3> pose;
      for (unsigned int j=0; j<nconstraints; ++j) {
         posesfile>>x>>y>>z;
         pose.push_back(Vector3(x,y,z));
      }
      displacements.push_back(pose);
   }
}

Motion *Motion::load(const char* filename) {
   
   ifstream motionfile(filename);
   if (motionfile.is_open()) {
      cout<<"motionfile.is_open()"<<endl;
      Motion *m = new Motion(filename);
      if (m->displacements.empty()) { cout<<"iffffffffffff"<<endl; delete m; return 0;}
      else { cout<<"elseeeeeeeee"<<endl; return m; }
   }
   
   return 0;
}
